package com.collection.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static String ymd = "yyyy-MM-dd";    
    public static SimpleDateFormat ymdSDF = new SimpleDateFormat(ymd);    
    
    public static String getNextMonthDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, 1);
        return ymdSDF.format(calendar.getTime());
    }
    public static String getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, 10);
        return ymdSDF.format(calendar.getTime());
    }
    
    /**
     * 当前日期
     * @return
     */
    public static String getCurrentDate(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return ymdSDF.format(calendar.getTime());
    }
    /**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(Date smdate,Date bdate) throws ParseException {    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        smdate=sdf.parse(sdf.format(smdate));  
        bdate=sdf.parse(sdf.format(bdate));  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
       return Integer.parseInt(String.valueOf(between_days));           
    }
    public static void main(String[] args) {
        
        System.out.println(getNextDay(new Date()));
    }
    /**
	 * 日期类型转指定格式字符串日期
	 * @param dt	日期
	 * @param pattern	日期格式
	 * @return
	 * @throws Exception
	 */
	public static String getCurrentTime3Date(Date dt, String pattern)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String date = sdf.format(dt);// new Date(); 就是当前系统时间
		return date;
	}
	
	 public static String stampToDate(Long s){
	        String res;
	        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        Date date = new Date(s);
	        res = simpleDateFormat.format(date);
	        return res;
	    }
	 /**
	  * 获取上个月的第一天
	  */
	 public static String lastMonthOneDay(){
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
         //获取前月的第一天
         Calendar   cal_1=Calendar.getInstance();//获取当前日期 
         cal_1.add(Calendar.MONTH, -1);
         cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
         String firstDay = format.format(cal_1.getTime());
         return firstDay;
	 }
	 /**
	  * 获取上个月的最后一天
	  */
	 public static String lastMonthEndDay(){
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		 //获取前月的最后一天
         Calendar cale = Calendar.getInstance();   
         cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
         String lastDay = format.format(cale.getTime());
         return lastDay;
	 }
}
